#include "pprint.hpp"
#include "global.hpp"

void sympy::bxSymPyPrettyPrint(
    int nlhs, bxArray *plhs[],
    int nrhs, const bxArray *prhs[])
{
    using namespace baltam::py;
    if (nrhs != 1)
        bxErrMsgTxt("输入参数数量不正确。请输入一个 SymPy 对象。");
    PyObject *args[2];
    args[1] = (PyObject *)bxGetCStruct(pyobject_sid, prhs[0]);
    if (args[1] == nullptr)
        bxErrMsgTxt("请输入一个 SymPy 对象。");
    PyObject *pretty =
        pyobject_get_attr_string(sp, "pretty");
    PyObject *ps = pyobject_vectorcall_pack(pretty, args + 1, 1, 0);
    py_dec_ref(pretty);
    if (ps == nullptr)
        Raise_PyErr();
    PyObject *s = pyobject_str(ps);
    if (s == nullptr)
    {
        py_dec_ref(ps);
        Raise_PyErr();
    }
    const char *str = pyunicode_as_utf8(s);
    if (str)
        bxPrintf("%s\n", str);
    py_dec_ref(s);
    py_dec_ref(ps);
}